<?php

/* ******************************
 *	Request aJax actions
* ******************************
*/

require_once('../../includes/classes/core.php');
include('../../includes/classes/log.class.php');
$log 		= new log();
$action 	= $_REQUEST['act'];
$error		= '';
$data		= '';


//incomming
$incom_id				= $_REQUEST['id'];

$call_date				= $_REQUEST['call_date'];
$control_id				= $_REQUEST['control_id'];
$site_user_pin			= $_REQUEST['pin'];
$call_type_id			= $_REQUEST['call_type_id'];
$category_id			= $_REQUEST['category_id'];
$problem_date			= $_REQUEST['problem_date'];
$call_content			= $_REQUEST['call_content'];
$category_parent_id 	= $_REQUEST['category_parent_id'];
$call_status_id			= $_REQUEST['call_status_id'];
$problem_comment		= $_REQUEST['problem_comment'];
$source					= $_REQUEST['source'];

$pay_type_id			= $_REQUEST['pay_type_id'];
$bank_id				= $_REQUEST['bank_id'];
$bank_object_id			= $_REQUEST['bank_object_id'];
$card_type_id			= $_REQUEST['card_type_id'];
$card_type1_id 			= $_REQUEST['card_type1_id'];
$pay_aparat_id			= $_REQUEST['pay_aparat_id'];
$object_id				= $_REQUEST['object_id'];
$money					= $_REQUEST['money'];


// site_user
$personal_pin			= $_REQUEST['personal_pin'];
$personal_id			= $_REQUEST['personal_id'];
$personal_phone			= $_REQUEST['personal_phone'];
$mail				    = $_REQUEST['mail'];
$name				    = $_REQUEST['name'];
$operator_name			= $_REQUEST['operator_name'];



//task
$persons_id			    = $_REQUEST['persons_id'];
$phone		    		= $_REQUEST['phone'];
$task_type_id			= $_REQUEST['task_type_id'];
$priority_id			= $_REQUEST['priority_id'];
$comment 	        	= $_REQUEST['comment'];
$problem_id 	        = $_REQUEST['problem_id'];
$task_department_id 	= $_REQUEST['task_department_id'];
$planned_date			= $_REQUEST['planned_date'];
$call_duration			= $_REQUEST['call_duration'];
$c_date					= $_REQUEST['c_date	'];
$fact_end_date			= $_REQUEST['fact_end_date'];
$template_id			= $_REQUEST['template_id'];
$hidden_inc				= $_REQUEST['hidden_inc'];
$edit_id				= $_REQUEST['edit_id'];
$delete_id				= $_REQUEST['delete_id'];
			


// file
$rand_file			= $_REQUEST['rand_file'];
$file				= $_REQUEST['file_name'];


switch ($action) {
	case 'get_add_page':
		$number		= $_REQUEST['number'];
		$page		= GetPage($res='', $number);
		$data		= array('page'	=> $page);

		break;
	
	case 'get_edit_page':
		$page		= GetPage(Getincomming($incom_id),$incom_id);

		$data		= array('page'	=> $page);

		break;
	case 'get_list' :
		$count 		= $_REQUEST['count'];
		$hidden 	= $_REQUEST['hidden'];
		$user_id	= $_SESSION['USERID'];
		$user		= $_SESSION['USERID'];
		$group		= checkgroup($user);
		
		$filter = '';
		if ($group != 2 && $group != 3) {
			$res_row = mysql_fetch_assoc(mysql_query("SELECT 	crystal_users.id as person_id
														FROM 	`crystal_users`
														WHERE 	`crystal_users`.`id` =$user_id"));
			
			$filter = 'AND (task.responsible_user_id ='.$res_row[person_id].' or task.control_user_id ='. $res_row[person_id].')';
		}
		mysql_query("SET @i = 0;");
  		$rResult = mysql_query("SELECT	    `task`.id,
										if(ISNULL(task.incomming_call_id), task.`id`, task.incomming_call_id) AS datee,
										if(ISNULL(task.incomming_call_id), DATE_FORMAT(task.`date`,'%d/%m/%y <span style=\"font-weight: bold;\">%H:%i:%s</span>'), DATE_FORMAT(incomming_call.`date`,'%d/%m/%y <span style=\"font-weight: bold;\">%H:%i:%s</span>')) AS datee,
										if(ISNULL(task.incomming_call_id), task.`phone`, incomming_call.`phone`) AS datee,
										incomming_call.pin,
										incomming_call.pid,
										if(ISNULL(task.incomming_call_id), source.`name`, sou.`name` ) AS source,
										if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
  										if(ISNULL(task.incomming_call_id), task.`money`, incomming_call.`money` ) AS `money`,
										if(ISNULL(task.incomming_call_id), object.`name`, obj.`name` ) AS object,
										`crystal_users`.`name` ,
    									`crystal_users1`.`name` ,
										`status`.`call_status`,
										TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration), '%i:%s')AS `duration`,
										CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>შემომავალი</button>') AS `file_name`
	                            FROM 					`task`
	                            LEFT JOIN 	incomming_call ON task.incomming_call_id=incomming_call.id
	                           	LEFT JOIN 	`category` 		ON task.category_id=category.id
	                            LEFT JOIN 	`category` AS cat 		ON incomming_call.call_subcategory_id=cat.id
	                            LEFT JOIN 	`object` 		ON task.object_id=object.id
	                            LEFT JOIN 	`object` AS obj 		ON incomming_call.object_id=obj.id
	                            LEFT JOIN 	source	AS sou	ON incomming_call.source_id=sou.id
	                            LEFT JOIN 	source ON task.task_source_id=source.id
	                            LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
	                            LEFT JOIN 	crystal_users   AS `crystal_users1`	ON task.user_id=crystal_users1.id
	                            LEFT JOIN 	`status`  	ON	task.`status`=`status`.`id`
								LEFT JOIN asterisk_incomming ON incomming_call.uniqueid=asterisk_incomming.uniqueid
								WHERE 		task.actived=1 and task.status=0 $filter
	  			");
	  
		$data = array(
				"aaData"	=> array()
		);

	while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
		for ( $i = 0 ; $i < $count ; $i++ )
			{
				
				if($i==12){
				if 	($aRow[$i]=='გადაცემულია გასარკვევად') 			{	$row[] = '<div style="background: #FF5555; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გარკვევის პროცესშია') 	{	$row[] = '<div style="background:#FFFF55; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='მოგვარებულია, აცნობეთ') 	{	$row[] = '<div style="background: #B1EF87; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='დასრულებული') 	{	$row[] = '<div style="background: #33dd33; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 else	{ $row[] = $aRow[$i];}	
				
				}else	{ $row[] = $aRow[$i];}
			}
			$data['aaData'][] = $row;
		}
			
		break;
	case 'save_incomming':
	    
	    if($call_status_id==3){
	        mysql_query("UPDATE `task` SET
	        `viewed`='1'
	        WHERE   `id`='$incom_id'");
	    }
	    
		$incom_id = $_REQUEST['id'];
		if($incom_id == ''){
			
			Addtask($persons_id, $c_date, $task_type_id, $template_id, $task_department_id, $call_type_id, $category_parent_id, $category_id, $problem_date, $call_status_id, $object_id, $comment, $planned_date, $fact_end_date, $call_duration, $priority_id, $problem_id, $pay_type_id, $bank_id, $pay_aparat_id, $card_type_id, $rand_file, $file, $hidden_inc);
			$task_id = mysql_insert_id();
		}else {
			Savetask($incom_id, $persons_id, $control_id, $phone, $planned_date, $fact_end_date, $call_duration, $problem_date,$call_status_id, $task_type_id, $priority_id, $task_department_id, $call_type_id, $category_id, $category_parent_id,  $object_id, $pay_type_id, $bank_id, $card_type_id,$pay_aparat_id,$comment, $problem_comment, $source, $money, $rand_file, $file);	
		}
		break;
		
	case 'get_calls':
	
		$data		= array('calls' => getCalls());
	
		break;
		
	case 'delete_file':
		
		mysql_query("DELETE FROM file WHERE id = $delete_id");
		
		$increm = mysql_query("	SELECT  `name`,
										`rand_name`,
										`id`
								FROM 	`file`
								WHERE   `task_id` = $edit_id
							  ");
		
		$data1 = '';
		
		while($increm_row = mysql_fetch_assoc($increm))	{
			$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
				          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
 					  </tr>';
		}
		
		$data = array('page' => $data1);
		
		break;
				
	case 'up_now':
		$user		= $_SESSION['USERID'];
		if($rand_file != ''){
			mysql_query("INSERT INTO 	`file`
									( 	`user_id`,
										`task_id`,
										`name`,
										`rand_name`
									)
									VALUES
									(	'$user',
										'$edit_id',
										'$file',
										'$rand_file'
									);");
			GLOBAL $log;
			$log->setInsertLog('file');
		}
		
		$increm = mysql_query("	SELECT  `name`,
										`rand_name`,
										`id`
								FROM 	`file`
								WHERE   `task_id` = $edit_id
							  ");
		
		$data1 = '';
		
		while($increm_row = mysql_fetch_assoc($increm))	{
			$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
				          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
 					  </tr>';
		}
		
		$data = array('page' => $data1);
			
		break;
		
	case 'sub_category':
		
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getcategory1($cat_id));
		
		break;	
		
	case 'sub_bank_category':
	
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getbank_object($cat_id));
	
		break;
		
	case 'disable':
		$incom_id				= $_REQUEST['id'];
		mysql_query("			UPDATE 	`task`
								SET 	`actived`=0
								WHERE 	`id`=$incom_id ");
		break;
	case 'get_add_info':
	
		$pin	=	$_REQUEST['pin'];
		$data 	= 	array('info' => get_addition_all_info($pin));
	
		break;
	case 'get_add_info1':
	
		$personal_id	=	$_REQUEST['personal_id'];
		$data 	= 	array('info1' => get_addition_all_info1($personal_id));
	
		break;
		
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Request Functions
* ******************************
*/
function checkgroup($user){
	$res = mysql_fetch_assoc(mysql_query("
											SELECT crystal_users.group_id
											FROM    crystal_users
											WHERE  crystal_users.id = $user
			"));
	return $res['group_id'];

}
     
function Savetask($incom_id, $persons_id, $control_id, $phone, $planned_date, $fact_end_date, $call_duration, $problem_date,$call_status_id, $task_type_id, $priority_id, $task_department_id, $call_type_id, $category_id, $category_parent_id,  $object_id, $pay_type_id, $bank_id, $card_type_id,$pay_aparat_id,$comment, $problem_comment, $source, $money, $rand_file, $file)
{	
	global $error;
	$user_id				= $_REQUEST['user_id'];
	$c_date					= date('Y-m-d H:i:s');
	$user  = $_SESSION['USERID'];
	$res_row = mysql_fetch_assoc(mysql_query("SELECT 	crystal_users.id as person_id
												FROM 	`crystal_users`
												WHERE 	`crystal_users`.`id` = $user"));
	
	
	
	
	
	if ($res_row[person_id]==$persons_id || $user=$user_id){
		
		
			mysql_query("UPDATE `task` 
							SET 	
									`control_user_id`		='$control_id', 
									`phone`					='$phone', 
									`planned_end_date`		='$planned_date', 
									`fact_end_date`			='$fact_end_date', 
									`call_duration`			='$call_duration', 
									`problem_date`			='$problem_date', 
									`call_status_id`		='$call_status_id', 
									`task_type_id`			='$task_type_id', 
									`priority_id`			='$priority_id', 
									`department_id`			='$task_department_id', 
									`call_type_id`			='$call_type_id', 
									`category_id`			='$category_parent_id', 
									`subcategory_id`		='$category_id', 
									`object_id`				='$object_id', 
									`pay_type_id`			='$pay_type_id', 
									`bank_id`				='$bank_id', 
									`card_type_id`			='$card_type_id', 
									`pay_aparat_id`			='$pay_aparat_id', 
									`comment`				='$comment', 
									`problem_comment`		='$problem_comment', 
									`status`				='$call_status_id', 
									`task_source_id`		='$source',
									`money`					='$money'
									`actived`='1' 
							WHERE (`id`='$incom_id');");
				
				}else 
				{
					$error='თქვენ არ გაქვთ შეცვლის უფლება';
				}	
			}
function Getcall_status($call_status_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `call_status`
						FROM 	`status`
						WHERE 	actived=1");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		
		if($res['id'] == $call_status_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['call_status'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['call_status'] . '</option>';
		}
	}
	return $data;
}

function Get_control($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getpay_type($pay_type_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`pay_type`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $pay_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Get_bank($bank_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`bank`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $bank_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
	
function Getbank_object($bank_object_id)
{  
	$data = '';
	$req = mysql_query("SELECT  id,
						     	`name`
						FROM 	bank_object
						WHERE 	bank_object.bank_id=$bank_object_id && actived =1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $bank_object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getbank_object_edit($bank_object_id)
{

	$data = '';
	$req = mysql_query("SELECT  id,
								`name`
						FROM 	bank_object
						WHERE 	bank_object.id=$bank_object_id && actived =1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $bank_object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
	
function Getcard_type($card_type_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`card_type`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $card_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getsource($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`source`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getcard_type1($card_type1_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`card_type`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $card_type1_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getpay_aparat($pay_aparat_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`pay_aparat`
					WHERE 	actived=1");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $pay_aparat_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getobject($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`object`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getcategory($category_id)

{ 			

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=0 ");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getcategory1($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
	
}

function Getcategory1_edit($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;

}

function Getcall_type($call_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					FROM `call_type`
					WHERE actived=1");
	
	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $call_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getdepartment($task_department_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `department`
					    WHERE actived=1 ");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_department_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getpriority($priority_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `priority`
						WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $priority_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Gettemplate($template_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `template`
						WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $template_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Gettask_type($task_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `task_type`
					    WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getpersons($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function getCalls(){
	

	$req = mysql_query("

						SELECT  	DISTINCT
									IF(SUBSTR(cdr.src, 1, 3) = 995, SUBSTR(cdr.src, 4, 9), cdr.src) AS `src`
						FROM    	cdr
						GROUP BY 	cdr.src
						ORDER BY 	cdr.calldate DESC
						LIMIT 		12


						");

	$data = '<tr class="trClass">
					<th class="thClass">#</th>
					<th class="thClass">ნომერი</th>
					<th class="thClass">ქმედება</th>
				</tr>
			';
	$i	= 1;
	while( $res3 = mysql_fetch_assoc($req)){

		$data .= '
	    		<tr class="trClass">
					<td class="tdClass">' . $i . '</td>
					<td class="tdClass" style="width: 30px !important;">' . $res3['src'] . '</td>
					<td class="tdClass" style="font-size: 13px !important;"><button class="insert" number="' . $res3['src'] . '">დამატება</button></td>
				</tr>';
		$i++;
	}

	return $data;

}
function get_addition_all_info($pin)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a1`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));
	
	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a2`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a3`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pin = $pin AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));
	
	$sum_incomming	 	= $res1['a'] + $res2['a1'] + $res3['a2'] + $res4['a3'];
	$sum_end	 		= $res1['a'] + $res2['a1'];
	$sum_actived		= $res3['a2'] + $res4['a3'];
	
	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table class="reqves_hist">
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a1'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოუგვარებელი</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a2'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a3'].'</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function get_addition_all_info1($personal_id)
{
	$res1 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res2 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a1`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` = 3 OR ISNULL(task.`status`))"));

	$res3 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a2`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 1 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$res4 = mysql_fetch_assoc(mysql_query("	SELECT COUNT(*) AS `a3`
											FROM 			`incomming_call`
											LEFT JOIN 		task ON incomming_call.id = task.incomming_call_id
											WHERE 			incomming_call.pid = $personal_id AND incomming_call.call_type_id = 2 AND (task.`status` != 3 AND NOT ISNULL(task.`status`))"));

	$sum_incomming	 	= $res1['a'] + $res2['a1'] + $res3['a2'] + $res4['a3'];
	$sum_end	 		= $res1['a'] + $res2['a1'];
	$sum_actived		= $res3['a2'] + $res4['a3'];

	$data .= '<fieldset>
					<legend>მომართვების ისტორია</legend>
					<table class="reqves_hist">
						<tr>
							<td>სულ</td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_incomming.'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოგვარებულები</td>
							<td></td>
							<td style="width: 150px;"></td>
						    <td>'.$sum_end.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'. $res1['a'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'.$res2['a1'].'</td>
						</tr>
						<tr>
							<td></td>
							<td>მოუგვარებელი</td>
							<td></td>
							<td style="width: 150px;"></td>
							<td>'.$sum_actived.'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>პრობლემა</td>
							<td style="width: 150px;"></td>
							<td>'.$res3['a2'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td>ინფორმაცია</td>
							<td style="width: 150px;"></td>
							<td>'. $res4['a3'].'</td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td></td>
							<td style="width: 150px;"></td>
				    		<td style="width: 300px;">
							<input type="button" value="ვრცლად"/>
		      				</td>
						</tr>
					</table>
				</fieldset>';

	return $data;
}
function Getincomming($incom_id){
$res = mysql_fetch_assoc(mysql_query("SELECT    	task.id AS id,
													if(ISNULL(task.incomming_call_id), task.`id`, incomming_call.`id` ) AS task_id,
													task.user_id,
													crystal_users.name as reqvester,
													incomming_call.uniqueid as inc_uniqueid,
													task.uniqueid as task_uniqueid,
													if(ISNULL(task.incomming_call_id), DATE_FORMAT(task.`date`,'%d/%m/%y %H:%i'), DATE_FORMAT(incomming_call.`date`,'%d/%m/%y %H:%i')) AS call_date,
													DATE_FORMAT(incomming_call.`date`,'%Y-%m-%d %H') AS record_date,
													if(ISNULL(task.incomming_call_id), task.`phone`, incomming_call.`phone` ) AS phone,
													if(ISNULL(task.incomming_call_id), task.`call_type_id`, incomming_call.`call_type_id` )  AS call_type_id,
													if(ISNULL(task.incomming_call_id), task.`task_source_id`, incomming_call.`source_id` ) AS `source`,
													if(ISNULL(task.incomming_call_id), task.`category_id`, incomming_call.`call_subcategory_id` ) AS category_id,
													task.status AS call_status_id,
													if(ISNULL(task.incomming_call_id), task.`subcategory_id`, incomming_call.`call_category_id` ) AS category_parent_id,
													if(ISNULL(task.incomming_call_id), DATE_FORMAT(task.`problem_date`,'%d/%m/%y %H:%i'), DATE_FORMAT(incomming_call.`problem_date`,'%d/%m/%y %H:%i') ) AS problem_date,
													if(ISNULL(task.incomming_call_id), task.`pay_type_id`, incomming_call.`pay_type_id` ) AS pay_type_id,
													if(ISNULL(task.incomming_call_id), task.`bank_id`, incomming_call.`bank_id` ) AS bank_id,
													if(ISNULL(task.incomming_call_id), task.`bank_object_id`, incomming_call.`bank_object_id` ) AS bank_object_id,
													if(ISNULL(task.incomming_call_id), task.`card_type_id`, incomming_call.`card_type_id` ) AS card_type_id,
													if(ISNULL(task.incomming_call_id), task.`pay_aparat_id`, incomming_call.`pay_aparat_id` ) AS pay_aparat_id,
													if(ISNULL(task.incomming_call_id), task.`object_id`, incomming_call.`object_id` ) AS object_id,
													if(ISNULL(task.incomming_call_id), task.`comment`, incomming_call.`call_content` ) AS `comment`,
													if(ISNULL(task.incomming_call_id), task.`money`, incomming_call.`money` ) AS `money`,
													task.`phone` AS task_phone,
													task.control_user_id,
													incomming_call.pid AS personal_id,
													incomming_call.pin AS personal_pin,
												  	task.task_type_id AS task_type_id,
													CONCAT(DATEDIFF(task.`fact_end_date`, task.`date`),' ','დღე და',' ',TIMEDIFF(DATE_FORMAT(task.`fact_end_date`,'%H:%i:%s'),DATE_FORMAT(task.`date`,'%H:%i:%s')),' ', 'სთ') as duration,
													task.responsible_user_id AS persons_id,
													task.priority_id AS priority_id,
													task.department_id AS task_department_id,
													DATE_FORMAT(task.`planned_end_date`,'%d/%m/%y %H:%i') as planned_end_date,
													DATE_FORMAT(task.fact_end_date,'%d/%m/%y %H:%i') as fact_end_date,
													task.call_duration,
													task.`problem_comment` AS `problem_comment`
																						
										FROM 	   	`task`
										LEFT JOIN incomming_call ON task.incomming_call_id=incomming_call.id
										LEFT JOIN crystal_users ON crystal_users.id=task.user_id
										WHERE      	task.id =$incom_id
			" ));
	
	return $res;
}

function GetPage($res='', $number, $incom_id)
{
	$num = 0;
	if($res[phone]==""){
		$num=$number;
	}else{ 
		$num=$res[phone]; 
	}
	
	$tanxa = 'class="hidden dialog-form-table"';
	$disabled = '';
	$hidden_class = '';
	
	if($_REQUEST['id'] == ''){
		$hidden_class = 'class="hidden"';
	}else{
		if($res[category_id] == 407){
			$tanxa = 'class="dialog-form-table"';
		}else{
			$tanxa = 'class="hidden dialog-form-table"';
		}
		$disabled = 'disabled="disabled"';
	}
	
	$increm = mysql_query("	SELECT  `name`,
	 								`rand_name`,
									`id`
							FROM 	`file`
							WHERE   `task_id` = $res[id]
							  ");
	
	
	$data  .= '
	<!-- jQuery Dialog -->
    <div id="add-edit-goods-form" title="საქონელი">
    	<!-- aJax -->
	</div>
	<div id="dialog-form">
			<div style="float: left; width: 500px;">	
				<fieldset >
			    	<legend>ძირითადი ინფორმაცია</legend>
					<tr>
						<label for="req_num">დამფორმირებელი</label>
						<label for="req_num">:</label>
						<label for="req_num">' .  $res['reqvester']. '</label>
					</tr>
		
			    	<table width="100%" class="dialog-form-table">
						<tr>
							<td style="width: 215px;"><label for="req_num">დავალების №</label></td>
							<td style="width: 215px !important;"><label for="req_data">თარიღი</label></td>
							<td style="width: 215px;"><label for="req_data">ტელეფონი</label></td>
						</tr>						
						<tr>
							<td style="width: 215px;">
								<input  style="width: 180px; type="text" id="id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['id']. '" disabled="disabled" />
							</td>
							<td style="width: 215px;">
								<input style="width: 180px; type="text" id="c_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' .  $res['call_date']. '" disabled="disabled" />
							</td>
							<td style="width: 215px;">		
							<input style="width: 180px; type="text" id="phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' .  $res['phone']. '" disabled="disabled" />
							</td>
							
						</tr>
					</table>';
									
		$data .= '<table id="additiona" class="dialog-form-table" width="100%">				
									
						<tr>
							<td style="width: 215px;"><label for="d_number">ზარის ტიპი</label></td>
							<td style="width: 215px;"><label for="d_number">კატეგორია</label></td>
							<td style="width: 215px;"><label for="d_number">ქვე-კატეგოტია</label></td>
							
						</tr>
						<tr>
							<td style="width: 215px;"><select style="width: 186px;" id="call_type_id" class="idls object"" >'. Getcall_type($res['call_type_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="category_parent_id" class="idls object"" >'. Getcategory($res['category_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="category_id" class="idls object"" >'. Getcategory1_edit($res['category_parent_id']).'</select></td>
							
						</tr>
						</table>
						<table id="additiona3" '.$tanxa.' width="100%">
						<tr id="aaaa">
							<td style="width: 215px;"><label id="pey_label1" for="d_number">ტრანზაქციის ფორმა</label></td>
							<td style="width: 215px;"><label for="d_number" id="bank_label">მომსახურე ბანკი</label></td>
							<td style="width: 215px;"><label id="pey_label"  for="d_number" >აპარატის ტიპი</label></td>
							
						</tr>
						<tr id="c">
							<td style="width: 215px;"><select style="width: 186px;" id="pay_type_id" class="idls object">'. Getpay_type($res['pay_type_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="bank_id" class="idls object">'. Get_bank($res['bank_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="pay_aparat_id" class="idls object">'. Getpay_aparat($res['pay_aparat_id']).'</select></td>

						</tr>
						<tr id="bbbb">
							<td style="width: 215px;"><label for="d_number" id="card_label">ბარათის ტიპი</label></td>
							<td style="width: 180px;"><label for="d_number">თანხის რაოდენობა</label></td>
							<td style="width: 215px;"></td>
							
						</tr>
						<tr id="d">
							<td style="width: 215px;"><select style="width: 186px;" id="card_type_id" class="idls object">'. Getcard_type($res['card_type_id']).'</select></td>
							<td style="width: 180px;">
								<input type="text" id="money" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['money'] . '" />
							</td>
							<td style="width: 215px;"></td>
						
						</tr>
					</table>
					<table id="additiona2" class="dialog-form-table" width="100%">	
						<tr>
							<td style="width: 215px;"><label for="req_num">პრობლემის თარიღი</label></td>
							<td style="width: 215px;"><label for="d_number">სტატუსი</label></td>
							<td style="width: 215px;"><label for="d_number">ობიექტი</label></td>			
						</tr>
						<tr>	
							<td style="width: 215px;">
								<input style="width: 180px;" type="text" id="problem_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res[problem_date] . '""  />
							</td>
							<td style="width: 215px;"><select style="width: 186px;" id="call_status_id" class="idls object">'. Getcall_status($res['call_status_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="object_id" class="idls object">'. Getobject($res['object_id']).'</select></td>	
								
						</tr>
						<tr>
							<td style="width: 215px;"><label for="task_type_id">დავალების ტიპი</label></td>
							<td style="width: 215px;"><label for="task_department_id">განყოფილება</label></td>
							<td style="width: 215px;"><label for="persons_id">პასუხისმგებელი პირი</label></td>
						</tr>
						<tr>
							<td style="width: 215px;"><select style="width: 186px;" id="task_type_id" class="idls object">'.Gettask_type($res['task_type_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="task_department_id" class="idls object">'. Getdepartment($res['task_department_id']).'</select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="persons_id" class="idls object">'.Getpersons($res['persons_id']).'</select></td>
						</tr>
						  <tr>
							<td style="width: 215px;"><label for="task_type_id">ინფორმაციის წყარო</label></td>
							<td style="width: 215px;"><label id="pr" class="hidden" for="d_number">პრიორიტეტები</label></td>
							<td style="width: 215px;"><label for="d_number">მაკონტროლებელი</label></td>
						</tr>
						<tr>
							<td style="width: 215px;"><select style="width: 186px;" id="source" class="idls object">'.Getsource($res['source']).'</select></td>
							<td style="width: 215px;"><label class="hidden" id="priority"><select id="priority_id" class="idls" >'.Getpriority($res['priority_id']).'</label></select></td>
							<td style="width: 215px;"><select style="width: 186px;" id="control_id" class="idls object">'.Get_control($res['control_user_id']).'</select></td>
						</tr>
						<tr>
							<td style="width: 215px;"><label for="task_type_id">სხვა ნომერი</label></td>
							<td style="width: 215px;"></td>
							<td style="width: 215px;"></td>
						</tr>
						<tr>
							<td style="width: 215px;">
								<input style="width: 180px; type="text" id="task_phone" class="idle" onblur="this.className=\'idle\'" value="' .  $res['task_phone']. '"disabled="disabled" />
							</td>
							<td style="width: 215px;"></td>
							<td style="width: 215px;"></td>
						</tr>
						</table>
						<table class="dialog-form-table" width="100%">
						<tr>
							<td style="width: 215px;"><label for="req_num">შესრ.პერ.დასაწყისი</label></td>
							<td style="width: 215px !important;"><label for="req_data">შესრ.პერ.დასასრული</label></td>
							<td style="width: 215px;"><label for="req_phone">შესრულების დრო</label></td>
						</tr>
						<tr>
							<td style="width: 215px;">
								<input type="text" id="planned_date" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['call_date']. '"disabled="disabled" />
							</td>
							<td style="width: 215px;">
								<input type="text" id="fact_end_date" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' .  $res['fact_end_date']. '"/>
							</td>
							<td style="width: 215px;">
								<input type="text" id="call_duration" style="width: 180px;" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['duration'] . '"disabled="disabled" />
							</td>
						</tr>
						</table>			
							<br>	
						<table>			
						<tr>
							<td style="width: 150px;"><label for="content">საუბრის შინაარსი</label></td>
							<td style="width: 150px;"><label for="content"></label></td>
							<td style="width: 150px;"><label for="content"></label></td>
						</tr>
						<tr>
							<td colspan="6">
								<textarea style="width: 641px; resize: none;" id="comment" class="idle" name="content" cols="300" rows="4">' . $res['comment'] . '</textarea>
							</td>
						</tr>
						<tr>
							<td '.$hidden_class.' style="width: 215px;"><label for="content">პრობლემის გადაწყვეტა</label></td>
						</tr>
						<tr>
							
							<td colspan="5">	
								<textarea '.$hidden_class.' style="width: 641px; resize: none;" id="problem_comment" class="idle" name="call_content" cols="300" rows="4">' . $res['problem_comment'] . '</textarea>
							</td>
						</tr>	
					</table>';		
									
		
												
		$data  .= '
				</fieldset >
		   
				
			</div>
			<div>
				  </fieldset>
			</div>
			<div style="float: right;  width: 355px;">
				 <fieldset>
					<legend>მომართვის ავტორი</legend>
					<table style="height: 134px;">
						<tr>
							<td style="width: 215px;">PIN კოდი</td>
							<td style="width: 215px;" class="hidden friend">მეგობრის PIN კოდი</td>
						</tr>
						<tr>
							<td style="width: 215px;"><input type="text" id="personal_pin" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_pin']  . '" /></td>
							<td style="width: 215px;" class="hidden friend">
								<input type="text" id="req_phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $num1 . '" />
							</td>
						</tr>
						<tr>
							<td style="width: 215px;">პირადი ნომერი</td>
							<td style="width: 215px;"></td>
						</tr>
						<tr>
							<td style="width: 215px;">
								<input type="text" id="personal_id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_id'] . '" />
							</td>
							<td style="width: 215px;"></td>
						</tr>
					</table>
				</fieldset>
				<div id="additional_info">';
					if (!empty($res['personal_pin'])){
							$data .= get_addition_all_info($res['personal_pin']);
						}
  $data .= '</div>';
	  
	  		$data .= GetRecordingsSection1($res);
	  		$data .= GetRecordingsSection($res);
	  		
		$data .= '		<fieldset>
					<legend>დამატებითი ინფორმაცია</legend> 
					<table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
					<tr>
						<td>
							<div class="file-uploader">
								<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
								<button id="choose_button" class="download">აირჩიეთ ფაილი</button>
								<input id="hidden_inc" type="text" value="'. increment('incomming_call') .'" style="display: none;">
							</div>
						</td>
					</tr>
				</table>
			     <table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
			          <tr style="border-bottom: 1px solid #85b1de;">
			           <td colspan="3">მიმაგრებული ფაილი</td>
			          </tr>
				</table>
				<table id="file_div" style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">';
					
					while($increm_row = mysql_fetch_assoc($increm))	{	
						$data .=' 
								        <tr style="border-bottom: 1px solid #85b1de;">
								          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
								          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
								          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
								        </tr>';
					}
			         
	 $data .= '
	 		</table>
				</fieldset>
	 		<input type="hidden" id="user_id" value="' . $res['user_id'] . '" />
			</div>
    </div>';

	return $data;
}

function GetRecordingsSection($res)
{
	if ($res[phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
	$req = mysql_query("SELECT  	DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_incomming.file_name AS `userfield`
						FROM 		asterisk_incomming
						WHERE		asterisk_incomming.uniqueid ='$res[inc_uniqueid]'");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 155px; float: right;">
            <legend>შემომავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}
	
	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download" str="' . $src . '">შემომავალი</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';
	
	return $data;
}
function GetRecordingsSection1($res)
{
	if ($res[phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
		$req = mysql_query("SELECT  DATE_FORMAT(asterisk_outgoing.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_outgoing.file_name AS `userfield`
							FROM 	asterisk_outgoing
							WHERE 	asterisk_outgoing.uniqueid='$res[task_uniqueid]'");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 154px; float: right;">
            <legend>გამავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}

	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download1" str="' . $src . '">გამავალი</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';

	return $data;
}
function increment($table){
	
	$result   		= mysql_query("SHOW TABLE STATUS LIKE '$table'");
	$row   			= mysql_fetch_array($result);
	$increment   	= $row['Auto_increment'];
	$next_increment = $increment+1;
	mysql_query("ALTER TABLE '$table' AUTO_INCREMENT=$next_increment");

	return $increment;
}
?>
